<?php
namespace app\api\controller;
use app\common\BaseController;
use think\Db;
use app\api\model\Receipt as ReceiptModel;
use app\api\model\Payment as PaymentModel;
use app\api\model\Transfer as TransferModel;

class PayFlow extends BaseController{    
    public function __construct(){
        parent::__construct();       
    }
    public function index(){         
        $pwhere=[];      
        $fwhere=[];
        $params = $this->request->param();       
        $page = 1;
        $pageCount = 10;        
        $datas=[];
        if(!empty($params)){      
             //按收付款日期查询
             if(!empty($params['start_date']) && !empty($params['end_date'])){             
                $pwhere['payment_date'] = [['>=',$params['start_date']],['<=',$params['end_date']]];
                $fwhere['transfer_date'] = [['>=',$params['start_date']],['<=',$params['end_date']]];
            }else{
                if(!empty($params['start_date'])){               
                    $pwhere['payment_date'] = ['>= ',$params['start_date']];                  
                    $fwhere['transfer_date'] = ['>= ',$params['start_date']];  
                }
                if(!empty($params['end_date'])){                
                    $pwhere['payment_date'] = ['<=',$params['end_date']];                 
                    $fwhere['transfer_date'] = ['<=',$params['end_date']];  
                }
            }     
           
            //页码            
            $page = !empty($params['page']) ? $params['page'] : $page;
            $pageCount = !empty($params['page_count']) ? $params['page_count'] : $pageCount;
              
            //按供应商查询
            if(!empty($params['supplier_id'])){
                $pwhere['supplier_id'] = $params['supplier_id'];             
            }   
            //按科目查询
            if(!empty($params['subject_id'])){            
                $pwhere['subject_id'] = $params['subject_id'];  
                $fwhere['from_bank'] = $params['subject_id'];        
            }         
        }  
        $Payment = new PaymentModel();
       $Receipt = new ReceiptModel();
        $Transfer = new TransferModel();
         
        $a = $Payment->alias('a')
        ->field("a.in,a.payment_date as price_date,a.subject_id,a.money,a.invoice_num,a.remark,b.supplier_id as x_id,b.supplier_id,c.start_date,c.sn,c.title")
        ->join('lt_team_outlay b',['a.outlay_id=b.id','a.type=b.type'])
        ->join('lt_team c','b.parent_id=c.id')
        ->where($pwhere)        
        ->buildSql();     
        $b = $Payment->alias('a')
        ->field("a.in,a.payment_date as price_date,a.subject_id,a.money,a.invoice_num,a.remark,b.supplier_id as x_id,b.supplier_id,b.start_date,b.sn,b.product_name as title")
        ->join('lt_single b',['a.outlay_id=b.id','a.type=b.type'])       
        ->where($pwhere)      
        ->buildSql();        
        $c = $Transfer->alias('a')
        ->field("a.from_in,a.transfer_date as price_date,a.from_bank as subject_id,a.from_money as money,a.from_number as invoice_num,x_id as remark,x_id,x_id as supplier_id,a.start_date,a.sn,a.title")        
        ->where($fwhere)      
        ->buildSql();  
         $datas = Db::table($a.' a')->union([$b,$c])->page("$page,$pageCount")->orderRaw("CAST(invoice_num as decimal)")->select();          
     
         $c_a = $Payment->alias('a')
        ->field("a.in,a.payment_date as price_date,a.subject_id,a.money,a.invoice_num,a.remark,b.supplier_id as x_id,c.start_date,c.sn,c.title")
        ->join('lt_team_outlay b',['a.outlay_id=b.id','a.type=b.type'])
        ->join('lt_team c','b.parent_id=c.id')
        ->where($pwhere)        
        ->count();     
        $c_b = $Payment->alias('a')
        ->field("a.in,a.payment_date as price_date,a.subject_id,a.money,a.invoice_num,a.remark,b.supplier_id as x_id,b.start_date,b.sn,b.product_name as title")
        ->join('lt_single b',['a.outlay_id=b.id','a.type=b.type'])       
        ->where($pwhere)      
        ->count();     
        $c_c = $Transfer->alias('a')
        ->field("a.from_in,a.transfer_date as price_date,a.from_bank as subject_id,a.from_money as money,a.from_number as invoice_num,x_id as remark,x_id,a.start_date,a.sn,a.title")        
        ->where($fwhere)      
        ->count();     
        $datalist = array(
            'datas' => $datas,            
            'dataCount' => $c_a+$c_b+$c_c
        );
        return $datalist;     
    }   
    public function cvs(){         
        $pwhere=[];      
        $fwhere=[];
        $params = $this->request->param();       
        $page = 1;
        $pageCount = 10;        
        $datas=[];
        if(!empty($params)){      
             //按收付款日期查询
             if(!empty($params['start_date']) && !empty($params['end_date'])){             
                $pwhere['payment_date'] = [['>=',$params['start_date']],['<=',$params['end_date']]];
                $fwhere['transfer_date'] = [['>=',$params['start_date']],['<=',$params['end_date']]];
            }else{
                if(!empty($params['start_date'])){               
                    $pwhere['payment_date'] = ['>= ',$params['start_date']];                  
                    $fwhere['transfer_date'] = ['>= ',$params['start_date']];  
                }
                if(!empty($params['end_date'])){                
                    $pwhere['payment_date'] = ['<=',$params['end_date']];                 
                    $fwhere['transfer_date'] = ['<=',$params['end_date']];  
                }
            }     
           
            //页码            
            $page = !empty($params['page']) ? $params['page'] : $page;
            $pageCount = !empty($params['page_count']) ? $params['page_count'] : $pageCount;
              
            //按供应商查询
            if(!empty($params['supplier_id'])){
                $pwhere['supplier_id'] = $params['supplier_id'];             
            }   
            //按科目查询
            if(!empty($params['subject_id'])){            
                $pwhere['subject_id'] = $params['subject_id'];  
                $fwhere['from_bank'] = $params['subject_id'];        
            }         
        }  
        $Payment = new PaymentModel();
       $Receipt = new ReceiptModel();
        $Transfer = new TransferModel();
         
        $a = $Payment->alias('a')
        ->field("a.in,a.payment_date as price_date,a.subject_id,a.money,a.invoice_num,a.remark,b.supplier_id as x_id,b.supplier_id,c.start_date,c.sn,c.title")
        ->join('lt_team_outlay b',['a.outlay_id=b.id','a.type=b.type'])
        ->join('lt_team c','b.parent_id=c.id')
        ->where($pwhere)        
        ->buildSql();     
        $b = $Payment->alias('a')
        ->field("a.in,a.payment_date as price_date,a.subject_id,a.money,a.invoice_num,a.remark,b.supplier_id as x_id,b.supplier_id,b.start_date,b.sn,b.product_name as title")
        ->join('lt_single b',['a.outlay_id=b.id','a.type=b.type'])       
        ->where($pwhere)      
        ->buildSql();        
        $c = $Transfer->alias('a')
        ->field("a.from_in,a.transfer_date as price_date,a.from_bank as subject_id,a.from_money as money,a.from_number as invoice_num,x_id as remark,x_id,x_id as supplier_id,a.start_date,a.sn,a.title")        
        ->where($fwhere)      
        ->buildSql();  
         $datas = Db::table($a.' a')->union([$b,$c])->orderRaw("CAST(invoice_num as decimal)")->select();          
     
        return $datas;     
    }   
}
